package com.miao.sso.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.miao.sso.entity.TbUser;
import com.miao.sso.mapper.TbUserMapper;
import com.miao.sso.service.TbUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-28
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {


    /**
     * 验证
     * @param param
     * @param type
     * @return
     */
    public Boolean chech(String param,Integer type){
        EntityWrapper<TbUser> wrapper = new EntityWrapper<>();
        //TbUser record=new TbUser();
        //判断type是否是1-3的数字
        switch (type){
            case 1:
                wrapper.where("username={0}",param);
                //如果是1 传入的就是用户名
                //  record.setUsername(param);
                break;
            case 2:
                wrapper.where("phone={0}",param);
                //如果是2传入的就是手机号
                //record.setPhone(param);
                break;
            case 3:
                wrapper.where("email={0}",param);
                //如果是3 传入的就是邮箱
                //  record.setEmail(param);
                break;
            default:
                //如果都不是就是参数有误
                return null;
        }
        return super.selectOne(wrapper)==null;
    }

    /**
     * 登录
     * @return
     */
    public String doLogin(String username,String password){
        EntityWrapper<TbUser> wrapper = new EntityWrapper<>();
        wrapper.where("username={0} and password={1}",username,DigestUtils.md5Hex(password));
        TbUser tbUser = super.selectOne(wrapper);
        if(tbUser==null){
            //登录失败
            return null;
        }
        //登录成功
        String token=DigestUtils.md5Hex(username+System.currentTimeMillis());
        return token;

    }

    /**
     * 查询用户信息
     * @param token
     * @return
     */
    public TbUser queryUserByToken(String token){
        String key="TOKEN_"+token;
        return null;
    }

}
